import { clear_gun, get_gun } from 'base/api';
import { s_psn, g_db } from 'db/state';
import { put_log } from 'base/ctl/log';
import { msn_info, is_new } from 'use/info';
import { insert_box, calc_pos } from './cmd';
import { cline, urlTb, obj_str, obj_int, obj_float } from 'sui';

const ck_data = (psn, hok, herr) => {
    const tp = g_db.boxtp();

    if (!tp) {
        return herr("箱子类型错误");
    }

    const cmd = {
        "n": tp,
        "obj": "*",
        "opt": {
            "desc": true,
            "limit": 1,
            "and": [
                {"eq": "psn", "v": psn},
            ]
        }
    };

    urlTb(null, "get", cmd,
        r => {
            const d = r[tp]["data"][0];

            const val = {
                line: cline(),
                que: true,
                data: [{
                    psn: obj_str(d, "psn"),
                    wsn: obj_str(d, "wsn"),
                    k: {
                        usn: "auto",
                        tsn: obj_str(d, "tsn"),
                    },
                    v: {
                        state: obj_int(d, "state"),
                    }
                }]
            };

            if (tp === "inner") {
                val["tab"] = "india";
                val["data"][0]["v"]["val"] = obj_float(d, "india");
                urlTb(null, "chunk", val);

                val["tab"] = "indt";
                val["data"][0]["v"]["val"] = obj_float(d, "indt");
                urlTb(null, "chunk", val);

                val["tab"] = "inwidth";
                val["data"][0]["v"]["val"] = obj_float(d, "inwidth");
                urlTb(null, "chunk", val);
            } else if (tp === "outer") {
                val["tab"] = "outdia";
                val["data"][0]["v"]["val"] = obj_float(d, "outdia");
                urlTb(null, "chunk", val);

                val["tab"] = "outdt";
                val["data"][0]["v"]["val"] = obj_float(d, "outdt");
                urlTb(null, "chunk", val);

                val["tab"] = "outwidth";
                val["data"][0]["v"]["val"] = obj_float(d, "outwidth");
                urlTb(null, "chunk", val);
            }

            hok();
        },
        e => herr("没有找到数据"));
};

const do_ckcode = (stepnum, step, umd, watch) => {
    const psn = get_gun(step);
    if (!psn) {
        watch.startWatch();
        return;
    }

    put_log("编码", psn, "扫描轴承编码");

    clear_gun();
    s_psn(psn);

    umd.set("psn", psn);

    const herr = (e) => {
        umd.setErr(e);
        watch.startWatch();
    };

    if (psn.length < 8) {
        return herr("条码位数错误");
    }

    if (psn.length > 10) {
        return herr("条码位数错误");
    }

    const v_tray = g_db.boxtray();
    const v_level = g_db.boxlv();

    if (!v_tray || !v_level) {
        return herr("请先设置托盘规格和层数");
    }

    if (!is_new(psn)) {
        return herr("条码已经入箱");
    }

    const sum = g_db.sum() + 1;

    if (sum > v_tray * v_level) {
        return herr("已经达到最大装箱容量");
    }

    const pos = calc_pos(sum);

    ck_data(psn, () => {
        insert_box(g_db.box(), psn, pos, () => {
            msn_info(psn, pos);

            let num = g_db.num() + 1;

            g_db.s_num(num);
            g_db.s_sum(sum);

            umd.mset({
                sum: sum,
                num: num,
                err: "",
            });

            watch.startWatch();
        }, e => {
            umd.setErr("入箱出错, 请重新扫码");
            watch.startWatch();
        });
    }, e => herr(e));
};

export {do_ckcode};
